package com.zbf.ccros;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/6/21  17:24
 * 描述: 网关中解决跨域
 */
@Configuration
public class TestCors {

    @Bean
    public CorsFilter getCorsFilter(){

        CorsConfiguration corsConfiguration=new CorsConfiguration();
        corsConfiguration.setMaxAge(36000L);
        corsConfiguration.addAllowedHeader("");
        corsConfiguration.addAllowedOrigin("http://localhost:8080");
        corsConfiguration.addAllowedOrigin("http://localhost:8080");
        corsConfiguration.addAllowedMethod("POST");
        corsConfiguration.addAllowedMethod("GET");
        corsConfiguration.addAllowedMethod("OPTION");

        corsConfiguration.setAllowCredentials(true);

        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource=new UrlBasedCorsConfigurationSource();
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);

        return new CorsFilter(urlBasedCorsConfigurationSource);

    }
}
